package org.example.socket;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SocketServe {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket;
        InputStream is;
        OutputStream os;
        //用于读取流中数据
        byte[] bytes = new byte[1024];
        //用户缓存剩下的数据
        List<Byte> buffer = new ArrayList<>();
        //数据处理字节数组
        byte[] data;
        socket = serverSocket.accept();
        System.out.println(socket.getLocalAddress());
        is = socket.getInputStream();
        os = socket.getOutputStream();
        int len = -1;
        int dataLen;
        while ((len = is.read(bytes)) != -1){
            try {
                data = appendBuffer(buffer,bytes,len);
                dataLen = data.length;
                buffer.clear();
                int index = -1;
                int start = 0;
                while (true){
                    if (start >= dataLen){
                        break;
                    }
                    index = getIndex(data,40,start);
                    //丢包
                    if (index == -1){
                        break;
                    }
                    //数据长度四个字节不完整
                    if (index + 5 >=dataLen){
                        for (int i = index; i < dataLen; i++) {
                            buffer.add(data[i]);
                        }
                        break;
                    }
                    int length = Integer.parseInt(new String(data,index+1,4));
                    //数据长度不够
                    if (index + 5 + length >= dataLen){
                        for (int i = index; i < dataLen; i++) {
                            buffer.add(data[i]);
                        }
                        break;
                    }
                    start = index + length + 7;
                    String re = new String(data,index+5,length);
                    System.out.println(re);
                }
            } catch (Exception e) {
                //丢包
                e.printStackTrace();
            }
            os.write("收到".getBytes());
            os.flush();
        }
//        decode();

    }

    public static int getIndex(byte[] bytes,int target,int start){
        int index = -1;
        for (int i = start; i < bytes.length; i++) {
            if (bytes[i] == target){
                index = i;
                break;
            }
        }
        return index;
    }

    public static byte[] appendBuffer(List<Byte> buffer,byte[] bytes,int len){
        int size = buffer.size();
        byte[] re = new byte[size+len];
        for (int i = 0; i < size; i++) {
            re[i] = buffer.get(i);
        }
        if (len >= 0) {
            System.arraycopy(bytes, 0, re, size, len);
        }
        return re;
    }

    public static void decode(){
        String s = "0x28 0x30 0x33 0x39 0x30 0x7b 0x22 0x63 0x6f 0x6d 0x6d 0x61 0x6e 0x64 0x22 0x3a 0x20 0x31 0x2c 0x20 0x22 0x73 0x74 0x61 0x74 0x65 0x22 0x3a 0x20 0x31 0x2c 0x20 0x22 0x72 0x65 0x70 0x6f 0x73 0x74 0x4e 0x75 0x6d 0x22 0x3a 0x20 0x31 0x2c 0x20 0x22 0x6d 0x65 0x73 0x73 0x61 0x67 0x65 0x49 0x64 0x22 0x3a 0x20 0x22 0x31 0x32 0x33 0x31 0x22 0x2c 0x20 0x22 0x64 0x65 0x76 0x49 0x64 0x22 0x3a 0x20 0x31 0x31 0x31 0x31 0x31 0x31 0x31 0x31 0x31 0x31 0x31 0x2c 0x20 0x22 0x69 0x73 0x52 0x65 0x54 0x61 0x6b 0x65 0x22 0x3a 0x20 0x30 0x2c 0x20 0x22 0x69 0x64 0x65 0x6e 0x74 0x69 0x66 0x79 0x4e 0x75 0x6d 0x22 0x3a 0x20 0x32 0x2c 0x20 0x22 0x69 0x64 0x65 0x6e 0x74 0x69 0x66 0x79 0x22 0x3a 0x20 0x5b 0x7b 0x22 0x6d 0x6f 0x64 0x65 0x6c 0x49 0x64 0x22 0x3a 0x20 0x31 0x2c 0x20 0x22 0x63 0x6c 0x61 0x73 0x73 0x69 0x66 0x79 0x52 0x65 0x73 0x75 0x6c 0x74 0x22 0x3a 0x20 0x22 0x6d 0x61 0x73 0x6b 0x22 0x2c 0x20 0x22 0x63 0x6f 0x6e 0x66 0x69 0x64 0x65 0x6e 0x63 0x65 0x22 0x3a 0x20 0x30 0x2e 0x39 0x32 0x2c 0x20 0x22 0x6c 0x65 0x66 0x74 0x58 0x22 0x3a 0x20 0x32 0x35 0x30 0x2c 0x20 0x22 0x6c 0x65 0x66 0x74 0x59 0x22 0x3a 0x20 0x32 0x35 0x30 0x2c 0x20 0x22 0x69 0x64 0x65 0x6e 0x74 0x4c 0x65 0x6e 0x67 0x74 0x68 0x22 0x3a 0x20 0x31 0x30 0x30 0x2c 0x20 0x22 0x69 0x64 0x65 0x6e 0x74 0x57 0x69 0x64 0x74 0x68 0x22 0x3a 0x20 0x31 0x30 0x30 0x7d 0x2c 0x20 0x7b 0x22 0x6d 0x6f 0x64 0x65 0x6c 0x49 0x64 0x22 0x3a 0x20 0x31 0x2c 0x20 0x22 0x63 0x6c 0x61 0x73 0x73 0x69 0x66 0x79 0x52 0x65 0x73 0x75 0x6c 0x74 0x22 0x3a 0x20 0x22 0x6d 0x61 0x73 0x6b 0x22 0x2c 0x20 0x22 0x63 0x6f 0x6e 0x66 0x69 0x64 0x65 0x6e 0x63 0x65 0x22 0x3a 0x20 0x30 0x2e 0x39 0x32 0x2c 0x20 0x22 0x6c 0x65 0x66 0x74 0x58 0x22 0x3a 0x20 0x32 0x35 0x30 0x2c 0x20 0x22 0x6c 0x65 0x66 0x74 0x59 0x22 0x3a 0x20 0x32 0x35 0x30 0x2c 0x20 0x22 0x69 0x64 0x65 0x6e 0x74 0x4c 0x65 0x6e 0x67 0x74 0x68 0x22 0x3a 0x20 0x31 0x30 0x30 0x2c 0x20 0x22 0x69 0x64 0x65 0x6e 0x74 0x57 0x69 0x64 0x74 0x68 0x22 0x3a 0x20 0x31 0x30 0x30 0x7d 0x5d 0x7d 0x0 0x29";
        String[] bytess = s.split(" ");
        byte[] bytes = new byte[1024];
        for (int i = 0; i < bytess.length; i++) {
            String ss = bytess[i].substring(2);
            bytes[i] = (byte) Integer.parseInt(ss,16);
        }
        int index = -1;
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] == 40){
                index = i;
                break;
            }
        }
        int length = Integer.parseInt(new String(bytes,index+1,4));
        System.out.println(new String(bytes,index+5,length));
    }
}
